home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / IPCMD.C < prev    next >
C/C++ Source or Header  |  1989-12-11  |  6KB  |  277 lines

  1. /* IP-related user commands */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "internet.h"
  6. #include "timer.h"
  7. #include "netuser.h"
  8. #include "iface.h"
  9. #include "ip.h"
  10. #include "cmdparse.h"
  11.  
  12. int doipaddr(),doipstat(),dottl();
  13. extern char badhost[];
  14. struct cmds ipcmds[] = {
  15.     "address",    doipaddr,    0,    NULLCHAR,    NULLCHAR,
  16.     "status",    doipstat,    0,    NULLCHAR,    NULLCHAR,
  17.     "ttl",        dottl,        0,    NULLCHAR,    NULLCHAR,
  18.     NULLCHAR,    NULLFP,        0,
  19.         "?subcommands", NULLCHAR
  20. };
  21. doip(argc,argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.     return subcmd(ipcmds,argc,argv);
  26. }
  27. int
  28. doipaddr(argc,argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     int32 n;
  33.  
  34.     if(argc < 2) {
  35.         printf("%s\n",inet_ntoa(ip_addr));
  36.     } else if((n = resolve(argv[1])) == 0){
  37.         printf(badhost,argv[1]);
  38.         return 1;
  39.     } else
  40.         ip_addr = n;
  41.     return 0;
  42. }
  43. int
  44. dottl(argc,argv)
  45. char *argv[];
  46. {
  47.     if(argc < 2)
  48.         printf("%u\n",uchar(ip_ttl));
  49.     else
  50.         ip_ttl = atoi(argv[1]);
  51.     return 0;
  52. }
  53.  
  54. /* "route" subcommands */
  55. int doadd(),dodrop(),doinfo();
  56. static void dump1route();
  57. static struct cmds rtcmds[] = {
  58.     "add", doadd, 3,
  59.     "<dest addr>[/<bits>] <if name>|via [gateway] [metric]",
  60.     "Add failed",
  61.  
  62.     "drop", dodrop, 2,
  63.     "<dest addr>[/<bits>]",
  64.     "Not in table",
  65.  
  66.     "info", doinfo, 2,
  67.     "<dest addr>",
  68.     NULLCHAR,
  69.  
  70.     NULLCHAR, NULLFP, 0,
  71.     "?subcommands",
  72.     NULLCHAR
  73. };
  74.  
  75. /* Display and/or manipulate routing table */
  76. int
  77. doroute(argc,argv)
  78. int argc;
  79. char *argv[];
  80. {
  81.     if(argc < 2){
  82.         dumproute();
  83.         return 0;
  84.     }
  85.     return subcmd(rtcmds,argc,argv);
  86. }
  87. /* Add an entry to the routing table
  88.  * E.g., "add 1.2.3.4 ax0 5.6.7.8 3"
  89.  */
  90. int
  91. doadd(argc,argv)
  92. int argc;
  93. char *argv[];
  94. {
  95.     struct interface *ifp;
  96.     int32 dest;
  97.     int32 gateway = 0;
  98.     unsigned bits;
  99.     char *bitp;
  100.     int metric = 0;
  101.     struct route *rp,*rt_lookup();
  102.  
  103.     if(strcmp(argv[1],"default") == 0){
  104.         dest = 0;
  105.         bits = 0;
  106.     } else {
  107.         /* If IP address is followed by an optional slash and
  108.          * a length field, (e.g., 128.96/16) get it;
  109.          * otherwise assume a full 32-bit address
  110.          */
  111.         if((bitp = index(argv[1],'/')) != NULLCHAR){
  112.             *bitp++ = '\0';
  113.             bits = atoi(bitp);
  114.         } else
  115.             bits = 32;
  116.  
  117.         if((dest = resolve(argv[1])) == 0){
  118.             printf(badhost,argv[1]);
  119.             return 1;
  120.         }
  121.     }
  122.     if(strlen(argv[2]) >= 3 && strncmp(argv[2],"unreachable",strlen(argv[2])) == 0){
  123.         return rt_add(dest,bits,gateway,metric,NULLIF);
  124.     }
  125.     if(argc > 3){
  126.         if((gateway = resolve(argv[3])) == 0){
  127.             printf(badhost,argv[3]);
  128.             return 1;
  129.         }
  130.         if(strcmp(argv[2],"via")){
  131.             if((ifp = ifunit(argv[2])) == NULLIF){
  132.                 return 1;
  133.             }
  134.         } else {
  135.             if((rp = rt_lookup(gateway)) == NULLROUTE){
  136.                 printf("No route to %s\n",argv[3]);
  137.                 return 1;
  138.             }
  139.             if(rp->gateway)
  140.                 gateway = rp->gateway;
  141.  
  142.             ifp = rp->interface;
  143.         }
  144.     } else {
  145.         if((ifp = ifunit(argv[2])) == NULLIF){
  146.             return 1;
  147.         }
  148.     }
  149.     if(argc > 4)
  150.         metric = atoi(argv[4]);
  151.  
  152.     return rt_add(dest,bits,gateway,metric,ifp);
  153. }
  154. /* Drop an entry from the routing table
  155.  * E.g., "drop 128.96/16
  156.  */
  157. int
  158. dodrop(argc,argv)
  159. int argc;
  160. char *argv[];
  161. {
  162.     char *bitp;
  163.     unsigned bits;
  164.     int32 n;
  165.  
  166.     if(strcmp(argv[1],"default") == 0){
  167.         n = 0;
  168.         bits = 0;
  169.     } else {
  170.         /* If IP address is followed by an optional slash and length field,
  171.          * (e.g., 128.96/16) get it; otherwise assume a full 32-bit address
  172.          */
  173.         if((bitp = index(argv[1],'/')) != NULLCHAR){
  174.             *bitp++ = '\0';
  175.             bits = atoi(bitp);
  176.         } else
  177.             bits = 32;
  178.  
  179.         if((n = resolve(argv[1])) == 0){
  180.             printf(badhost,argv[1]);
  181.             return 1;
  182.         }
  183.     }
  184.     return rt_drop(n,bits);
  185. }
  186. /* 
  187.  * Display route to a certain IP address
  188.  */
  189. int
  190. doinfo(argc,argv)
  191. int argc;
  192. char *argv[];
  193. {
  194.     int32 dest;
  195.     struct route *rp,*rt_lookup();
  196.     
  197.     if((dest = resolve(argv[1])) == 0){
  198.         printf(badhost,argv[1]);
  199.         return 1;
  200.     }
  201.  
  202.     if((rp = rt_lookup(dest)) == NULLROUTE){
  203.         printf("No route to %s\n",argv[1]);
  204.         return 1;
  205.     }
  206.  
  207.     printf("%s: ",inet_ntoa(dest));
  208.     dump1route(rp);
  209.     return 0;
  210. }
  211.  
  212. /* Dump IP routing table
  213.  * Dest             Length    Interface    Gateway         Metric
  214.  * 192.001.002.003   32           sl0        192.002.003.004      4
  215.  */
  216. int
  217. dumproute()
  218. {
  219.     register unsigned int i,bits;
  220.     register struct route *rp;
  221.  
  222.     printf("Dest              Length    Interface    Gateway          Metric\n");
  223.     for(bits=0;bits<=32;bits++){
  224.         for(i=0;i<NROUTE;i++){
  225.             for(rp = routes[bits][i];rp != NULLROUTE;rp = rp->next){
  226.                 printf("%-18s",(bits == 0)?"default":inet_ntoa(rp->target));
  227.                 printf("%-10u",bits);
  228.                 dump1route(rp);
  229.             }
  230.         }
  231.     }
  232.     return 0;
  233. }
  234.  
  235. static void
  236. dump1route (rp)
  237. register struct route *rp;
  238.  
  239. {
  240.     if(rp->interface == NULLIF){
  241.         printf("unreachable\n");
  242.     } else {
  243.         printf("%-13s",rp->interface->name);
  244.         printf("%-17s",(rp->gateway != 0)? inet_ntoa(rp->gateway):"");
  245.         printf("%6u\n",rp->metric);
  246.     }
  247. }
  248.  
  249. int
  250. doipstat(argc,argv)
  251. int argc;
  252. char *argv[];
  253. {
  254.     extern struct ip_stats ip_stats;
  255.     extern struct reasm *reasmq;
  256.     register struct reasm *rp;
  257.     register struct frag *fp;
  258.  
  259.     printf("IP: total %ld runt %u len err %u vers err %u",
  260.         ip_stats.total,ip_stats.runt,ip_stats.length,ip_stats.version);
  261.     printf(" chksum err %u badproto %u\n",
  262.         ip_stats.checksum,ip_stats.badproto);
  263.  
  264.     if(reasmq != NULLREASM)
  265.         printf("Reassembly fragments:\n");
  266.     for(rp = reasmq;rp != NULLREASM;rp = rp->next){
  267.         printf("src %s",inet_ntoa(rp->source));
  268.         printf(" dest %s",inet_ntoa(rp->dest));
  269.         printf(" id %u pctl %u time %lu len %u\n",
  270.             rp->id,uchar(rp->protocol),rp->timer.count,rp->length);
  271.         for(fp = rp->fraglist;fp != NULLFRAG;fp = fp->next){
  272.             printf(" offset %u last %u\n",fp->offset,fp->last);
  273.         }
  274.     }
  275.     return doicmpstat();
  276. }
  277.